var express = require('express');
var router = express.Router();
// var query = require("../db/db").query
var queryWithPromise = require("../db/db").queryWithPromise

/* GET home page. */
router.get('/', async function(req, res, next) {
  // 从前端传来的参数
  console.log(req.query);
  // 1. 获取city表总数 total
  let sql_city_count = 'select count(*) from city';
  let count_res = await queryWithPromise(sql_city_count,[]); // {err,results,fields}
  let total = count_res.results[0]["count(*)"];// 获取列表的总数
  // 2. 根据 req.query 数据 { start:"0",pageSize:'20'} 获取分页数据
  let sql_city_list = "select * from city LIMIT ?,?;"
  let values = [parseInt(req.query.start),parseInt(req.query.pageSize)];
  let city_list_res = await queryWithPromise(sql_city_list,values);
  let data = [];
  console.log(city_list_res);
  city_list_res.results.map(v=>{
    data.push({
        ID:v.ID,
        Name:v.Name,
        Population:v.Population
    })
  })
  res.json({
      code:1,
      total:total,
      data:data
  })
});

module.exports = router;